package com.guoguo.ztree.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.guoguo.ztree.entity.Group;
import com.guoguo.ztree.entity.Paging;
import com.guoguo.ztree.service.GroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class GroupController {
    private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-ss HH:mm:ss").create();
    @Autowired
    private GroupService groupService;

    /**
     * 部门列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/group/list")
    public String listByName(HttpServletRequest request, Group group, @RequestParam(defaultValue = "1") long pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        Paging<Group> paging = groupService.getGroupListPage(group, pageNum, pageSize);
        request.setAttribute("zNodes", gson.toJson(paging.getDataList()));
        request.setAttribute("paging", paging);
        request.setAttribute("group", group);
        return "group-list";
    }

    /**
     * 部门列表
     *
     * @param parentId
     * @return
     */
    @RequestMapping("/group/ajax/list")
    @ResponseBody
    public String list(@RequestParam("parentId") Long parentId) {
        List<Group> groupList = groupService.getGroupListByParentId(parentId);
        return gson.toJson(groupList);
    }

    /**
     * 批量添加部门
     *
     * @return
     */
    @RequestMapping("/group/add")
    @ResponseBody
    public Object add() {
        return groupService.batchAdd();
    }
}
